//
//  WmcFeedbackAPI.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/1.
//

import Foundation
import YPNetwork
import RxSwift

struct WmcFeedbackAPI {
    
    
    /// 获取反馈信息
    /// - Parameters:
    ///   - appId: 业务id
    ///   - configId: 模板id
    static func loadFeedbackConfigInfo(appId: Int, configId: Int) -> Single<[WmcFeedbackItem]> {
        var params: [String: Any] = [:]
        params["appId"] = appId
        params["configId"] = configId
        let obs: Observable<YPNetworkResponse> = WmcMediaRequest.requestData(path: "/crm/v1/feedbackConfig/info", parameters: params) { request in
            request.method = .post
        }
        let itemsObs = obs.map { res -> [WmcFeedbackItem] in
            guard let dic = res.data as? [String: Any],
                  let array = dic["classify"] as? [[String: Any]],
                  let items = [WmcFeedbackItem].deserialize(from: array) else {
                return []
            }
            return items.compactMap { $0 }
        }
        return itemsObs.asSingle()
    }
    
    /// 提交反馈信息
    /// - Parameters:
    ///   - appId: 业务id
    ///   - configId: 模板id
    ///   - classificationId: 分类id
    ///   - content: 反馈描述
    static func postFeedbackRecord(appId: Int, configId: Int, classificationId: Int?, content: String?) -> Single<YPNetworkResponse> {
        var params: [String: Any] = [:]
        params["appId"] = appId
        params["configId"] = configId
        params["classificationId"] = classificationId
        params["content"] = content
        return WmcMediaRequest.requestData(path: "/crm/v1/feedbackRecord/add", parameters: params) { request in
            request.method = .post
        }.asSingle()
    }
}
